using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 挂载在能对话的 NPC 上
/// </summary>
public class DialogueController : MonoBehaviour
{
    public DialogueData_SO dialogueData;
    public bool canTalk;
    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player") && dialogueData != null)
        {
            canTalk = true;
        }
    }
    private void Update()
    {
        if (canTalk && Input.GetMouseButtonDown(1))
        {
            OpenDialogue();
        }
    }

    private void OpenDialogue()
    {
        // 打开UI 传输对话内容
        DialogueUI.Instance.UpdateDialogueData(dialogueData);
        DialogueUI.Instance.UpdateMainDialogue(dialogueData.dialoguePieces[0]);
    }
    /// <summary>
    /// 脱离范围后，关闭对话且按键时不会触发对话
    /// </summary>
    /// <param name="other"></param>
    private void OnTriggerExit(Collider other) {
        if (other.CompareTag("Player"))
        {
            DialogueUI.Instance.dialoguePanel.SetActive(false);
            canTalk = false;
        }
    }
}
